package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.data.GroupProfitData;
import com.banzhi.shop.model.CallBack.GroupProfitCallBack;
import com.banzhi.shop.model.IGroupProfitModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/11/25.
 */

public class GroupProfitImpl2 implements IGroupProfitModel {
    @Override
    public void getGroupProfitData(String type, String page, String pagesize, final GroupProfitCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.GroupProfitURL);
        params.addBodyParameter("tt","1");
        params.addBodyParameter("type", type);
        params.addBodyParameter("page", page);
        params.addBodyParameter("pagesize", pagesize);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.e(" GroupProfitImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtils.e("Success:",result.toString());
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        GroupProfitData groupProfitData = gson.fromJson(result, GroupProfitData.class);
                        callBack.ongetGroupProfitDataSuccess(groupProfitData.getData());
                    }else {
                        callBack.ongetGroupProfitDataError(jsonObject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                //报错的位置
                LogUtils.e("GroupProfitImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                //    ToastUtils.makeToastShort("网络加载失败,请稍后再试(111)");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
